# 功能模块：3D引擎主文件
# 作者：dl
# 描述：引擎动作逻辑和业务逻辑入口

from vtkmodules.vtkRenderingCore import (
  vtkRenderWindow,
  vtkRenderWindowInteractor,
)
from visualizationCom.mouseInteractorEvent import mouseInteractorEvent

# 新加业务逻辑
from visualizationCom.bussiness.factory import factoryScene
from visualizationCom.bussiness.workshop import workshopScene
from visualizationCom.bussiness.productionline import productlineScene
from visualizationCom.bussiness.device import deviceScene
from visualizationCom.bussiness.product import productScene

# 创建线程，用于加载亿级点云模型
import threading

class lusterVisualization:
    # 构造函数
    def __init__(self, state, ctrl):
        # 可暴露的属性均在此处设置，切勿在其他地方定义，避免逻辑混乱
        self.renderWindow = None

        self.webState = state
        self.webCtrl = ctrl

        self.vtkInit()

    def vtkInit(self):
        self.factory = factoryScene(self, self.webState, self.webCtrl)
        self.workshop = workshopScene(self, self.webState, self.webCtrl)
        self.productline = productlineScene(self, self.webState, self.webCtrl)
        self.product = productScene(self, self.webState, self.webCtrl)
        self.device = deviceScene(self, self.webState, self.webCtrl)

        # 创建一个vtkRenderWindow对象，并将产线场景作为初始场景
        self.renderWindow = vtkRenderWindow()
        self.renderWindow.AddRenderer(self.productline.renderer)
        self.productline.engine.SetRenderWindow(self.renderWindow)

        # 创建一个vtkRenderWindowInteractor对象
        self.interactor = vtkRenderWindowInteractor()
        self.interactor.SetRenderWindow(self.renderWindow)

        # 设置交互器样式,这个是之前自己写的，现在用石老师的，避免每次都要修改代码
        style = mouseInteractorEvent(
            self, self.webState, self.webCtrl)
        style.SetDefaultRenderer(self.productline.renderer)
        style.SetOutlineRenderer(self.productline.engine.GetOutlineRenderer())
        self.interactor.SetInteractorStyle(style)

        self.mouseInteractorEvent = style  # 前端需要触发里面的事件

        self.interactor.Initialize()

        # 设置默认帧数
        self.renderWindow.SetDesiredUpdateRate(30)

    def run(self):
        # 程序启动的第一个场景会导致与GMQM系统的通信出错，因此需标记第一次进入的场景
        self.productline.enterScene(True)

    def startThread(self):
        my_thread = threading.Thread(target=self.thread_function)
        # 启动线程
        my_thread.start()

    def thread_function(self):
        self.product.preload('8020-钢件')